﻿Public Class frmMEpsArp

    Public arrConsulta(2) As String
    Private arrCampos() As String = {"epsarp_id", "epsarp_nombre", "epsarp_descri"}

    Private Sub frmMEpsArp_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mdiPrincipal.tsmiPruebas.Enabled = True
    End Sub

    Private Sub frmEpsArp_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        refreshList()
    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("EpsArp")

        For i As Integer = 0 To 2
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= If(strCondicion <> "", " AND ", "") & If(i = 0, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If
        Next

        mdlCRUD.loadRecord(dstRecord, "EpsArp", "epsarp", strCondicion, Nothing)
        Me.dgvEpsArp.DataSource = dstRecord.Tables("EpsArp")

        Me.dgvEpsArp.Columns(0).HeaderText = "Codigo"
        Me.dgvEpsArp.Columns(1).HeaderText = "Nombre"
        Me.dgvEpsArp.Columns(2).HeaderText = "Descripción"
        Me.dgvEpsArp.Columns(0).Width = 60
        Me.dgvEpsArp.Columns(1).Width = 200
        Me.dgvEpsArp.Columns(2).Width = 200
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvEpsArp.Rows(Me.dgvEpsArp.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            Me.tsbEditar.Enabled = False
            renderForm("Editar - Eps Arp", 3)
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Eps Arp")
        End If
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbNuevo.Click
        Me.tsbNuevo.Enabled = False
        Me.renderForm("Nuevo - Eps Arp", 1)
    End Sub

    Private Sub tsbRefresh_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbRefresh.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        Me.refreshList()

    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)
        Dim frmEpsArp As New frmEpsArp

        frmEpsArp.MdiParent = mdiPrincipal
        frmEpsArp.frmParent = Me

        Dim dstRecord As New DataSet

        dstRecord.Tables.Add("EpsArp")
        mdlCRUD.loadRecord(dstRecord, "EpsArp", "epsarp", "", "epsarp_id")

        frmEpsArp.modeForm = intMode
        frmEpsArp.Text = strTitle
        frmEpsArp.Show()

    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbConsultar.Click
        Me.tsbConsultar.Enabled = False
        Me.renderForm("Consultar - Eps Arp", 2)
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar - Eps Arp")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvEpsArp.Rows(Me.dgvEpsArp.CurrentRow.Index).Cells(0).Value.ToString

            mdlCRUD.delRecord("epsarp", "epsarp_id = '" & strId & "'")

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub

End Class